package com.example.homework;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.homework.model.Contact;

import java.util.List;

public class ContactArrayAdapter extends ArrayAdapter<Contact> {
    private Context context;
    private int resource;
    public ContactArrayAdapter(@NonNull Context context, int resource) {
        super(context, resource);
        this.context=context;
        this.resource=resource;
    }

    public void setContacts(List<Contact> contacts) {
        for (Contact contact:contacts){
            add(contact);
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        Contact contact = getItem(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(resource, parent, false);
        }
        TextView contactName = convertView.findViewById(R.id.contact_name);
        contactName.setText(contact.getName());
        return convertView;
    }
}
